package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanageovertimealarm;
import com.example.demo.handle.OvertimeAlarmListParseHandler;
import com.example.demo.mapper.PersonnelmanageovertimealarmMapper;
import com.example.demo.service.PersonnelmanageovertimealarmService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-23
 */
@Service
public class PersonnelmanageovertimealarmServiceImpl extends ServiceImpl<PersonnelmanageovertimealarmMapper, Personnelmanageovertimealarm> implements PersonnelmanageovertimealarmService {

    @Autowired
    private PersonnelmanageovertimealarmMapper personnelmanageovertimealarmMapper;

    @Override
    public Integer insertAlarmData(String alarmXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            OvertimeAlarmListParseHandler overtimeAlarmListParseHandler = new OvertimeAlarmListParseHandler();
            try {
                // 读取xml文件
                parser.parse(alarmXml, overtimeAlarmListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + overtimeAlarmListParseHandler.getPersonnelmanageovertimealarmArrayList().size() + "个节点被存储");
            for (Personnelmanageovertimealarm personnelmanageovertimealarm : overtimeAlarmListParseHandler.getPersonnelmanageovertimealarmArrayList()) {
                System.out.println(personnelmanageovertimealarm.getCsMineCode());
                System.out.println(personnelmanageovertimealarm.getCsDataTime());
                System.out.println(personnelmanageovertimealarm.getPsPersonCard());
                System.out.println(personnelmanageovertimealarm.getPsMineEntertime());
                System.out.println(personnelmanageovertimealarm.getPsMineOuttime());
                System.out.println(personnelmanageovertimealarm.getPsAreaCode());
                System.out.println(personnelmanageovertimealarm.getPsAreaEntertime());
                System.out.println(personnelmanageovertimealarm.getPsStationCode());
                System.out.println(personnelmanageovertimealarm.getPsStationEntertime());
                System.out.println("----finish----");
                personnelmanageovertimealarmMapper.insertAlarmData(personnelmanageovertimealarm.getCsMineCode(),personnelmanageovertimealarm.getCsDataTime(),personnelmanageovertimealarm.getPsPersonCard(),personnelmanageovertimealarm.getPsMineEntertime(),personnelmanageovertimealarm.getPsMineOuttime(),personnelmanageovertimealarm.getPsAreaCode(),personnelmanageovertimealarm.getPsAreaEntertime(),personnelmanageovertimealarm.getPsStationCode(),personnelmanageovertimealarm.getPsStationEntertime());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
